package com.imau.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imau.bean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public interface UserMapper extends BaseMapper<User> {

    /**
     *
     * 重写父接口方法，为外键多表关联
     * @param id
     * @return
     */
    @Override
    User selectById(Serializable id);

    /**
     * 按照角色id查询所有用户
     */
    List<User> getUserListByRoleid(@Param("rid") Object rid);

    /**
     * 按照删除用户所有角色
     */
    @Delete("delete  from t_user_role where user_id = #{uid}")
    int deleteRole(@Param("uid") Object uid);

    /**
     * 添加角色 维护中间表插入用户角色
     */
    @Insert(" insert into t_user_role (role_id, user_id)  values (#{roleid}, #{uid})")
    int addRole(@Param("uid") String uid, @Param("roleid") Serializable roleid);














    @Select("select * from t_user  where id =#{id}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "dept", column = "department_id", one = @One(select = "com.imau.mapper.DepartmentMapper.getById", fetchType = FetchType.LAZY)),
            @Result(property = "roles", column = "id", many = @Many(select = "listrole", fetchType = FetchType.LAZY))
    })
    User getById(@Param("id") Object id);

    public List<User> getUserListByDid(@Param("did") Object did);


    @Select("select * from t_user where username = #{uname} and password =#{upass} ")
    public User getByUnamePass(@Param("uname") String uname, @Param("upass") String upass);


    @Insert("insert into t_user (id,mobile,username,password,enable_state,create_time,department_id,update_time) " +
            "values (#{bean.id},#{bean.mobile},#{bean.username},#{bean.password},#{bean.enableState},#{bean.createTime},#{bean.dept},#{bean.updateTime} )")
    int addByMap(@Param("bean") Map<String, Object> bean);


    int updateByMap(Map<String, Object> bean);


    int updateRole(@Param("uid") String uid, @Param("roleid") String roleid);


}
